$(function(){

	//按钮一
    var type,year,month,date,textName,searchTimmer,optionType,dataTime,fuText;

    //根据type值请求
    var searchb = function()
    {
    	clearTimeout(searchTimmer);
    	searchTimmer = setTimeout(function(){
    		    	type = $('.dataval:visible').data('type');
    		    	if(type == 1)
    			    {	
    			    	year = $('.year').val();
    			    	month = $('.month1').val();
    			    	optionType = $('.optionType1').val();
					    if(optionType == 0)
						{
							$('.reportYear').hide();
							$('.reportMonth').hide();
							url = "/modules/operatingStatistics/turnover.html?type="+type;
						}
					    else if(optionType == 1)
				    	{
				    		$('.reportYear').show();
							$('.reportMonth').hide();
				    		date = year;
				    		url = "/modules/operatingStatistics/turnover.html?type="+type+"&date="+date;
				    	}
				    	else if(optionType == 2)
		    			{
		    				$('.reportYear').show();
							$('.reportMonth').show();
				    		date = year + "-"+month;
				    		url = "/modules/operatingStatistics/turnover.html?type="+type+"&date="+date;
		    			}
    			    	textName = $(".type[name=type]").find("option:selected").text();
    			    	ajaxUrl2(url,type,textName);
    			    }
    			    else if(type == 2)
    			    {
    			    	year = $('.year2').val();
    			    	month = $('.month2').val();
    			    	optionType = $('.optionType2').val();
					    if(optionType == 0)
						{
							$('.reportYear').hide();
							$('.reportMonth').hide();
							url = "/modules/operatingStatistics/turnover.html?type="+type;
						}
					    else if(optionType == 1)
				    	{
				    		$('.reportYear').show();
							$('.reportMonth').hide();
				    		date = year;
				    		url = "/modules/operatingStatistics/turnover.html?type="+type+"&date="+date;
				    	}
				    	else if(optionType == 2)
		    			{
		    				$('.reportYear').show();
							$('.reportMonth').show();
				    		date = year + "-"+month;
				    		url = "/modules/operatingStatistics/turnover.html?type="+type+"&date="+date;
		    			}
    			    	textName = $(".type2[name=type]").find("option:selected").text();
    			    	ajaxUrl2(url,type,textName);
    			    }
    			    else if(type == 3)
    			    {
    			    	year = $('.year3').val();
    			    	month = $('.month3').val();
    			    	optionType = $('.optionType3').val();
					    if(optionType == 0)
						{
							$('.reportYear').hide();
							url = "/modules/operatingStatistics/cumulativeBorrow.html?type="+type;
						}
					    else if(optionType == 1)
				    	{
				    		$('.reportYear').show();
				    		date = year;
				    		url = "/modules/operatingStatistics/cumulativeBorrow.html?type="+type+"&date="+date;
				    	}
    			    	textName = $(".type3[name=type]").find("option:selected").text();
    			   		ajaxUrl2(url,type,textName);
    			    }
    			    else if(type == 4)
			    	{
			    		year = $('.year4').val();
			    		month = $('.month4').val();
			    		optionType = $('.optionType4').val();
					    if(optionType == 0)
						{
							$('.reportYear').hide();
							url = "/modules/operatingStatistics/cumulativeTender.html?type=" + type;
						}
					    else if(optionType == 1)
				    	{
				    		$('.reportYear').show();
				    		date = year;
				    		url = "/modules/operatingStatistics/cumulativeTender.html?type=" + type + "&date=" + date;
				    	}
			    		textName = $(".type4[name=type]").find("option:selected").text();
    			   		ajaxUrl2(url,type,textName);
			    	}
    	}, 200);
    	
    }
    
	var ajaxUrl2 = function (url,type,textName){
		$.ajax({
			url:url + "&randomTime=" + (new Date()).getTime(),
			type:"get",
			dataType:"json",
			cache:false,
			success:function(data){
				var option;
				if(type == 1)//type1
				{
					var sums = 0;
					for(var i=0;i<data.turnover.length;i++)
					{
						sums = sums+data.turnover[i];
						if(sums == 0)
						{
							fuText = '暂无数据';
						}
						else
						{
							fuText = '';
						}
					}
					if(optionType == 0||optionType == 2)
					{
						dataTime = data.years
					}
					else if(optionType == 1)
					{
						dataTime=['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'];
					}
					option = {
						title : {
						    text: textName,
						    x:'center',
			            	y:'top',
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					    	isSelected:true,
					    	show:true,
					    	x:'center',
					    	y:'bottom',
					        data:['成交额','环比值']
					    },
					    selectedMode: true,
					    xAxis : [
					        {
					            type : 'category',
					            data : dataTime
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value',
					            name : '成交额',
					            axisLabel : {
					                formatter: '{value} 元'
					            }
					        },
					        {
					            type : 'value',
					            name : '环比值',
					            axisLabel : {
					                formatter: '{value} %'
					            }
					        }
					    ],
					    series : [

					        {
					            name:'成交额',
					            type:'bar',
					            data: data.turnover
					        },
					        {
					            name:'环比值',
					            type:'line',
					            yAxisIndex: 1,
					            data: data.ratios
					        }
					    ]
					};
					echarts.init(document.getElementById('investTablea')).setOption(option); 				                    
				}
				else if(type == 2)//type2
				{
					var sums = 0;
					for(var i=0;i<data.turnover.length;i++)
					{
						sums = sums+data.turnover[i];
						if(sums == 0)
						{
							fuText = '暂无数据';
						}
						else
						{
							fuText = '';
						}
					}
					if(optionType == 0||optionType == 2)
					{
						dataTime = data.years
					}
					else if(optionType == 1)
					{
						dataTime=['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'];
					}
					option = {
						title : {
						    text: textName,
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					    	x:'center',
					    	y:'bottom',
					        data:['成交笔数','环比值']
					    },
					    xAxis : [
					        {
					            type : 'category',
					            data : dataTime
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value',
					            name : '成交笔数',
					            axisLabel : {
					                formatter: '{value} 笔'
					            }
					        },
					        {
					            type : 'value',
					            name : '环比值',
					            axisLabel : {
					                formatter: '{value} %'
					            }
					        }
					    ],
					    series : [

					        {
					            name:'成交笔数',
					            type:'bar',
					            data: data.turnover
					        },
					        {
					            name:'环比值',
					            type:'line',
					            yAxisIndex: 1,
					            data: data.ratios
					        }
					    ]
					};
			        echarts.init(document.getElementById('investTableb')).setOption(option); 					                    
				}
				else if(type == 3)//type3
				{
					var sums = 0;
					for(var i=0;i<data.cumulativeBorrow.length;i++)
					{
						sums = sums+data.cumulativeBorrow[i];
						if(sums == 0)
						{
							fuText = '暂无数据';
						}
						else
						{
							fuText = '';
						}
					}
					if(optionType == 0)
					{
						dataTime = data.years
					}
					else if(optionType == 1)
					{
						dataTime=['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'];
					}
					option = {
			        	color  : ['#70bc31'],
			            tooltip: {
			                show: true
			            },
			            title: {
			            	text: textName,
			            	x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
			            },
			            grid: {
			            	x:80,
			            	y:80,
			            	x2:30,
			            	y2:70
			           	},
			            xAxis : [
			                {
			                   type : 'category',
			                   data : dataTime
			                }
			            ],
			            yAxis : [
			                {
			                	type:'value',
			                    name:'人数（人）',
			                }
			            ],
			            series : [
			                {
			                    "name":"人数（人）",
			                    "type":"bar",
			                    "data":data.cumulativeBorrow
			                }
			            ]
			        };
					echarts.init(document.getElementById('investTablec')).setOption(option); 
				}
				else if(type == 4)//type4
				{
					var sums = 0;
					for(var i=0;i<data.cumulativeTender.length;i++)
					{
						sums = sums+data.cumulativeTender[i];
						if(sums == 0)
						{
							fuText = '暂无数据';
						}
						else
						{
							fuText = '';
						}
					}
					if(optionType == 0)
					{
						dataTime = data.years
					}
					else if(optionType == 1)
					{
						dataTime=['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'];
					}
					option = {
			        	color  : ['#70bc31'],
			            tooltip: {
			                show: true
			            },
			            title: {
			            	text: textName,
			            	x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
			            },
			            grid: {
			            	x:80,
			            	y:80,
			            	x2:30,
			            	y2:70
			           	},
			            xAxis : [
			                {
			                   type : 'category',
			                   data : dataTime
			                }
			            ],
			            yAxis : [
			                {
			                	type:'value',
			                    name:'人数（人）',
			                }
			            ],
			            series : [
			                {
			                    "name":"人数（人）",
			                    "type":"bar",
			                    "data":data.cumulativeTender
			                }
			            ]
			        };
					echarts.init(document.getElementById('investTabled')).setOption(option); 
				}
			}
		});
	};

	var changBtn = $('.investorsTabs .tabs-header ul li span');
	changBtn.click(function(){
		searchb();//初始化加载、、
	});
	searchb();
    $(".type,.type2,.type3,.type4,.year,.year2,.year3,.year4,.month1,.month2,.month3,.month4").change(function(){
    	searchb();
    });		        
});